﻿using Dapper;
using SIG.DAL.Dapper.Model;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;

namespace SIG.DAL.Dapper.Repositories
{

    public class LinkCategoryRepository
    {
        private DbConnection _connection;


        public IEnumerable<LinkCategory> GetItems()
        {
            IEnumerable<LinkCategory> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.GetList<LinkCategory>();
            }
            return result;
        }

        public IEnumerable<LinkCategory> GetActiveItems()
        {
            IEnumerable<LinkCategory> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.GetList<LinkCategory>(new { Active = true });
            }
            return result;
        }

        public IEnumerable<LinkCategory> GetListPagedItems(int pageIndex, int pageSize,string keyword, out int totalCount)
        {            
            IEnumerable<LinkCategory> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                keyword = !string.IsNullOrEmpty(keyword) ? keyword.Replace("[", "[[]").Replace("%", "[%]") : keyword;
                string likeKeyword = "%" + keyword + "%";

                totalCount = _connection.RecordCount<LinkCategory>("where Title like @Title", new { Title = likeKeyword });
                result = _connection.GetListPaged<LinkCategory>(pageIndex, pageSize, "where Title like @Title",  "Importance desc" , new { Title = likeKeyword });
            }
            return result;
        }

        public LinkCategory GetById(int id)
        {
            LinkCategory result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Get<LinkCategory>(id);
            }
            return result;
        }
        public int? Create(LinkCategory model)
        {
            int? result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Insert(model);
            }
            return result;
        }
        
        public int Update(LinkCategory model)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Update(model);
            }
            return result;
        }
        public int Delete(LinkCategory model)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Delete(model);
            }
            return result;
        }

        public int Delete(int id)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Delete<LinkCategory>(id);
            }
            return result;
        }


    }
}
